package com.kingmed.kmss.bpmn.flow.helper;

import java.util.List;

import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kingmed.kmss.common.bo.bpmn.GroupBO;
import com.kingmed.kmss.common.bo.bpmn.UserBO;
import com.kingmed.kmss.common.bo.bpmn.UserGroupBO;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class IdentityHelper {

	@Autowired
	IdentityService identityService;

	public boolean login(String userId, String password) {
		boolean result = this.identityService.checkPassword(userId, password);
		//
		if (result)
			log.info(" {}/{}-登录成功.", userId, password);
		else
			log.info(" {}/{}-登录失败.", userId, password);

		return result;
	}
	
	public List<User> getUserList() {
		return identityService.createUserQuery().list();
	}
	
	public User getUserById(String userId) {
		User user = identityService.createUserQuery().userId(userId).singleResult();
		return user;
	}
	
	public void createUser(UserBO userBO) {
		User user = identityService.newUser(userBO.getUserId());
		user.setDisplayName(userBO.getName());
		user.setEmail(userBO.getEmail());
		//user.setPassword(userBo.getPassword());
	
		identityService.saveUser(user);
		
		//assignUserToGroups(userBO);
	}
	
	public List<Group> getGroupList() {
		return identityService.createGroupQuery().list();
	}
	
	public List<User> getGroupUsers(String groupId) {
		return identityService.createUserQuery().memberOfGroup(groupId).list();
	}
	
	public List<Group> getGroupsByUserId(String userId) {
		return identityService.createGroupQuery().groupMember(userId).list();
	}
	
	public void createGroup(GroupBO groupBO) {
		Group group = identityService.newGroup(groupBO.getGroupId());
		group.setName(groupBO.getName());
		group.setType(groupBO.getType());
	
		identityService.saveGroup(group);
	}

	public void assignUserToGroups(UserBO userBo) {
		
		if (userBo.getGroups() != null && userBo.getGroups().size() > 0) {
			for (GroupBO groupBO : userBo.getGroups()) {
				if (StringUtils.isNotEmpty(groupBO.getGroupId())) {
					Group group = identityService.createGroupQuery().groupId(groupBO.getGroupId()).singleResult();
					
					
					if (group != null) {
						if (identityService.createUserQuery().memberOfGroup(group.getId()).userId(userBo.getUserId()).count() ==0) {
						identityService.createMembership(userBo.getUserId(), groupBO.getGroupId());				
						}
					}
				}
			}
		}
		
	}

	public void deleteUser(UserBO userBO) {
		List<Group> groups = identityService.createGroupQuery().groupMember(userBO.getUserId()).list();
		
		groups.forEach(g -> {
			identityService.deleteMembership(userBO.getUserId(), g.getId());
		});
		
		identityService.deleteUser(userBO.getUserId());
	}

	public void unassignUserFromGroup(UserGroupBO userGroupBO) {

		identityService.deleteMembership(userGroupBO.getUserId(), userGroupBO.getGroupId());

	}

	public void deleteGroup(GroupBO groupBO) {
		List<User> users = identityService.createUserQuery().memberOfGroup(groupBO.getGroupId()).list();
		users.forEach(u -> {
			identityService.deleteMembership(u.getId(), groupBO.getGroupId());
			
		});
		
		identityService.deleteGroup(groupBO.getGroupId());
	}
	
	public void setProcessInitator(String userId) {
		identityService.setAuthenticatedUserId(userId);
	}
}
